<?php
namespace backend\controllers;

use common\models\LoginForm;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;

/**
 * Site controller
 */
class SiteController extends Controller
{

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            //return $this->goHome();
        }

        $model           = new LoginForm();
        $reload          = 0;
        $captchaRequired = Yii::$app->session->get('captchaRequired');
        if ($captchaRequired >= 3) {
            $reload = 1;
            if ($captchaRequired > 3) {
                $model->setScenario('captchaLogin');
            }
        }

        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post(), '')) {
            $model->org_code = Yii::$app->request->post('org_code');
            $model->org_name = Yii::$app->request->post('org_name');

            if ($model->login()) {
                Yii::$app->session->set('captchaRequired', 0);
                $reffer = Yii::$app->request->post('reffer');
                if (!empty($reffer) && $reffer != Yii::$app->request->absoluteUrl && strpos($reffer,
                        'site/logout') === false) {
                    // Yii::$app->session->set('reffer', Yii::$app->request->post('reffer'));
                }

                return $this->renderJson(
                    [
                        'type' => 'success',
                        'url'  => Yii::$app->getHomeUrl(),
                    ]);
            } else {

                $errors = $model->errors;

                $errors_arr = [];
                foreach ($errors as $field => $err) {
                    $errors_arr = array_merge($errors_arr, $err);
                }

                return $this->renderJson(
                    [
                        'type'    => 'fail',
                        'message' => implode('<br>', $errors_arr),
                        'reload'  => $reload,
                    ]);
            }

        } else {
            return $this->renderPartial('login'
                , ['model' => $model,]
            );
        }
    }

}
